package com.jinlia.show.modules.wxapp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.common.core.utils.PageUtils;
import com.jinlia.show.modules.base.model.form.GoodsForm;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.model.query.GoodsTypeQuery;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.system.entity.GoodsType;
import com.jinlia.show.modules.base.system.entity.OrderItem;
import com.jinlia.show.modules.base.system.service.GoodsAttrMapperService;
import com.jinlia.show.modules.base.system.service.GoodsMapperService;
import com.jinlia.show.modules.base.system.service.GoodsTypeMapperService;
import com.jinlia.show.modules.base.system.service.OrderItemMapperService;
import com.jinlia.show.modules.wxapp.model.vo.GoodsTypeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class GoodsService extends GoodsMapperService {

    @Autowired
    private GoodsAttrMapperService goodsAttrMapperService;
    @Autowired
    private OrderItemMapperService orderItemMapperService;
    @Autowired
    private GoodsTypeMapperService goodsTypeMapperService;
    @Autowired
    private GoodsMapperService goodsMapperService;

    public Page<GoodsPO> goodsListPage(GoodsTypeQuery goodsTypeQuery) {
        Page<Goods> page = new Page<>(goodsTypeQuery.getCurrent(), goodsTypeQuery.getSize());
        //构造查询参数
        LambdaQueryWrapper<Goods> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Objects.nonNull(goodsTypeQuery.getGoodsTypeId()), Goods::getGoodsType, goodsTypeQuery.getGoodsTypeId());
        lambdaQueryWrapper.eq(Objects.nonNull(goodsTypeQuery.getEnableStatus()), Goods::getEnableStatus, goodsTypeQuery.getEnableStatus());
        Page<Goods> page1 = this.page(page, lambdaQueryWrapper);
        Page<GoodsPO> sysUserVOPage = PageUtils.tToVO(page1, GoodsPO.class);
        goodsAttrMapperService.fillGoods(sysUserVOPage.getRecords());
        return sysUserVOPage;
    }

    public List<GoodsPO> getHotCombination(Long id) {
        List<OrderItem> list = orderItemMapperService.lambdaQuery()
                .eq(OrderItem::getGoodsId, id)
                .last("limit 10")
                .list();
        Set<Long> collect = list.stream().map(item -> item.getOrderId()).collect(Collectors.toSet());
        List<OrderItem> listOrderItem = orderItemMapperService.lambdaQuery()
                .in(OrderItem::getOrderId, collect)
                .ne(OrderItem::getGoodsId, id)
                .list();
        List<Long> hotGoodsIds = listOrderItem.stream().map(item -> item.getGoodsId()).collect(Collectors.toList());
        GoodsForm goodsForm = new GoodsForm();
        goodsForm.setGoodsIds(hotGoodsIds);
        List<GoodsPO> voList = this.getBaseMapper().getVOList(goodsForm);
        return voList;
    }

    public List<GoodsTypeVO> getForOrder() {
        //
        List<GoodsTypeVO> goodsTypeVOList = goodsTypeMapperService.list().stream().map(i -> GoodsTypeVO.fromEntity(i)).collect(Collectors.toList());
        List<GoodsPO> voList = goodsMapperService.getBaseMapper().getVOList(new GoodsForm());
        Map<Long, List<GoodsPO>> collect =
                voList.stream().collect(Collectors.groupingBy(i -> i.getGoodsType()));
        goodsTypeVOList.forEach(i -> i.setListGoodsPO(collect.get(i.getId())));
        return goodsTypeVOList;
    }
}
